package com.qianfeng.biquge.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.biquge.constans.ResultCode;
import com.qianfeng.biquge.exceptions.AddDataException;
import com.qianfeng.biquge.exceptions.DeleteDataException;
import com.qianfeng.biquge.exceptions.QueryDataException;
import com.qianfeng.biquge.mapper.AuthorMapper;
import com.qianfeng.biquge.mapper.BookMapper;
import com.qianfeng.biquge.mapper.CategorieMapper;
import com.qianfeng.biquge.pojo.Authors;
import com.qianfeng.biquge.pojo.Books;
import com.qianfeng.biquge.pojo.Categories;
import com.qianfeng.biquge.service.BookService;
import com.qianfeng.biquge.vo.BooksVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    private CategorieMapper categorieMapper;


    @Override
    public Books addBook(Books books) {

        if (books==null || books.getBookname()==null || books.getAuthorid()==null || books.getCategoryid()==null){

            throw new AddDataException("添加图书失败,图书名不能为空", ResultCode.BOOKNAME_NULL);

        }

        bookMapper.addBook(books);

        Books book = bookMapper.getBookByBookId(books.getBookid());

        if (book==null){

            throw new AddDataException("添加图书失败", ResultCode.DATA_NOT_EXIST);

        }

        return book;


    }

    @Override
    public PageInfo<BooksVo> getAllBookByBookNameOrByAuthorName(String name1, int pageNum, int pageSize) throws UnsupportedEncodingException {



        String name = URLDecoder.decode(name1, "UTF-8");


        //设置分页数据
        PageHelper.startPage(pageNum, pageSize);

        //模糊查询所有的作者
        List<Authors> authorsList = authorMapper.getAuthorByAuthorName(name);

        //这个集合用来保存所有图书,并返回
        List<BooksVo> booksList = new ArrayList<>();

        //通过作者ID获取图书
        for (Authors authors : authorsList) {

            List<BooksVo> books1 = bookMapper.getAllBookByAuthorId(authors.getAuthorid());

            for (BooksVo books : books1) {

                books.setAuthor(authors);

            }

            booksList.addAll(books1);

        }

        //模糊查询所有的图书
        List<BooksVo> books = bookMapper.getAllBookByBookName(name);

        //根据作者ID查询作者对象,set到authors属性中
        for (BooksVo book : books) {

            Authors author = authorMapper.getAuthorByAuthorId(book.getAuthorid());

            book.setAuthor(author);

        }

        //将模糊查询到的图书存储到最终集合
        booksList.addAll(books);

        //查询所有的分类,存到属性中
        for (BooksVo booksVo : booksList) {

            Categories categorie = categorieMapper.getCategorieByCategoryId(booksVo.getCategoryid());
            booksVo.setCategorie(categorie);

        }

        PageInfo<BooksVo> pageInfo=new PageInfo<BooksVo>(booksList);

        return pageInfo;


    }

    @Override
    public int deleteBook(Long bookId) {

        if (bookId==null || bookId <= 0){

            throw new DeleteDataException("删除图书失败,ID不能为空",ResultCode.NULL_DATA);

        }

        int i = bookMapper.deleteBook(bookId);

        if (i<=0){

            throw new DeleteDataException("删除图书失败,ID不存在",ResultCode.DATA_NOT_EXIST);

        }

        return i;


    }

    @Override
    public List<BooksVo> getBookByAuthorid(Long authorId) {

        if (authorId==null || authorId <= 0){

            throw new QueryDataException("查询失败,ID不能为空",ResultCode.NULL_DATA);

        }

        List<BooksVo> booksList = bookMapper.getAllBookByAuthorId(authorId);

        if (booksList==null){

            throw new QueryDataException("查询失败",ResultCode.FAIL);

        }

        for (BooksVo booksVo : booksList) {

            Authors author = authorMapper.getAuthorByAuthorId(authorId);
            booksVo.setAuthor(author);

            Categories categorie = categorieMapper.getCategorieByCategoryId(booksVo.getCategoryid());
            booksVo.setCategorie(categorie);

        }

        return booksList;

    }

    @Override
    public List<BooksVo> getBookByAuthoridByBookId(Long authorId, Long bookId) {

        if (authorId==null || bookId==null){

            throw new QueryDataException("查询失败,ID不能为空",ResultCode.NULL_DATA);

        }

        List<BooksVo> booksList = bookMapper.getBookByAuthoridByBookId(authorId, bookId);

        if (booksList==null){

            throw new QueryDataException("查询失败",ResultCode.NULL_DATA);

        }

        for (BooksVo booksVo : booksList) {

            Authors author = authorMapper.getAuthorByAuthorId(booksVo.getAuthorid());
            booksVo.setAuthor(author);

            Categories categorie = categorieMapper.getCategorieByCategoryId(booksVo.getCategoryid());
            booksVo.setCategorie(categorie);

        }

        return booksList;

    }

    @Override
    public List<BooksVo> getBookByCategoryIdIsRandom(Long categoryId) {

        List<Long> longList = bookMapper.getAllBookIsCategoryId();

        Random random=new Random();
        int i = random.nextInt(longList.size());
        categoryId = longList.get(i);

        List<BooksVo> booksList = bookMapper.getBookByCategoryIdIsRandom(categoryId);

        if (booksList==null){

            throw new QueryDataException("查询失败",ResultCode.DATA_NOT_EXIST);

        }

        for (BooksVo booksVo : booksList) {

            Authors author = authorMapper.getAuthorByAuthorId(booksVo.getAuthorid());
            booksVo.setAuthor(author);

            Categories categorie = categorieMapper.getCategorieByCategoryId(booksVo.getCategoryid());
            booksVo.setCategorie(categorie);

        }

        return booksList;

    }
}
